package com.ruoyi.system.web.controller;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.web.domain.WebMenu;
import com.ruoyi.system.web.service.IWebMenuService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网站菜单Controller
 * 
 * @author ruoyi
 * @date 2023-11-21
 */
@Controller
@RequestMapping("/web/webMenu")
public class WebMenuController extends BaseController
{
    private String prefix = "web/webMenu";

    @Autowired
    private IWebMenuService webMenuService;

    @RequiresPermissions("web:webMenu:view")
    @GetMapping()
    public String webMenu()
    {
        return prefix + "/webMenu";
    }

    /**
     * 查询网站菜单列表
     */
    @RequiresPermissions("web:webMenu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebMenu webMenu)
    {
        startPage();
        webMenu.setDelFlag(0);
        List<WebMenu> list = webMenuService.selectWebMenuList(webMenu);
        return getDataTable(list);
    }

    /**
     * 导出网站菜单列表
     */
    @RequiresPermissions("web:webMenu:export")
    @Log(title = "网站菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebMenu webMenu)
    {
        List<WebMenu> list = webMenuService.selectWebMenuList(webMenu);
        ExcelUtil<WebMenu> util = new ExcelUtil<WebMenu>(WebMenu.class);
        return util.exportExcel(list, "网站菜单数据");
    }

    /**
     * 新增网站菜单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存网站菜单
     */
    @RequiresPermissions("web:webMenu:add")
    @Log(title = "网站菜单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebMenu webMenu)
    {
        return toAjax(webMenuService.insertWebMenu(webMenu));
    }

    /**
     * 修改网站菜单
     */
    @RequiresPermissions("web:webMenu:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WebMenu webMenu = webMenuService.selectWebMenuById(id);
        mmap.put("webMenu", webMenu);
        return prefix + "/edit";
    }

    /**
     * 修改保存网站菜单
     */
    @RequiresPermissions("web:webMenu:edit")
    @Log(title = "网站菜单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebMenu webMenu)
    {
        return toAjax(webMenuService.updateWebMenu(webMenu));
    }

    /**
     * 删除网站菜单
     */
    @RequiresPermissions("web:webMenu:remove")
    @Log(title = "网站菜单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        WebMenu webMenu = new WebMenu();
        webMenu.setId(Long.parseLong(ids));
        webMenu.setDelFlag(1);
        return toAjax(webMenuService.updateWebMenu(webMenu));
    }


    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
//        mmap.put("menu", webMenuService.selectWebMenuById(menuId));
        return prefix + "/webMenuTree";
    }


    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/webMenuTreeData")
    @ResponseBody
    public List<Ztree> webMenuTreeData()
    {
        Long userId = ShiroUtils.getUserId();
        List<Ztree> ztrees = webMenuService.menuTreeData(userId);
        return ztrees;
    }
}
